package com.vs.wms.srv.sysfun;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.vs.wms.dao.SysFunctionDao;
import com.vs.wms.dao.impl.SysFunctionDaoImpl;
import com.vs.wms.pojo.SysFunction;

/**
 * Servlet implementation class MenuServlet
 */
public class MenuServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		System.out.println(request.getParameter("id"));
		
		String parentId = request.getParameter("id");
		
		PrintWriter out = response.getWriter();
		
		if (!"root".equals(parentId)) {
			
			SysFunctionDao sysFunctionDao = new SysFunctionDaoImpl();
			SysFunction sysFun = new SysFunction();
			sysFun.setFunType("F");
			sysFun.setParentId(new Long(parentId));
			
			List<SysFunction> lstResult = sysFunctionDao.getSysFuns(sysFun);
			
			System.out.println(lstResult.size());
			
			JSONArray array = new JSONArray();
			
			if (lstResult != null && lstResult.size() > 0) {
				
				for (SysFunction sysFunction : lstResult) {
					
					Map<String, String> map = new HashMap<String, String>();
					map.put("id", sysFunction.getId());
					map.put("text", sysFunction.getFunName());
					map.put("leaf", "false");
					
					array.add(map);
				}
				
				System.out.println(array.toString());
				
				out.print(array.toString());
			}
		}
		
		out.flush();
		out.close();
	}
}
